package com.mars.miaosha.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author MARS
 * @date 2018/5/7
 */

@Service
public class RedisPoolFactory {
    @Autowired
    RedisConfig redisConfig;

    /**
     * 配置一个JedisPool 的 bean
     * @return
     */
    @Bean
    public JedisPool jedisPoolFactory(){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);

        /**
         * 参数依次为：jedisPool的配置，host，port，超时时间，密码，0库（redis一共16个库，默认从第0个库使用）
         */
        JedisPool jp = new JedisPool(poolConfig,redisConfig.getHost(),
                redisConfig.getPort(),redisConfig.getTimeout()*1000,
                redisConfig.getPassword(),
                0);
        return jp;
    }
}
